package org.pyframe.parser;

public class PacketTools {
	public static String nvl(String src) {
		return PacketTools.nvl(src, "");
	}
	
	public static String nvl(String src, String def) {
		if (src == null || "".equals(src)) {
			return def;
		} else {
			return src;
		}
	}

	public static String RPAD(String str, int len, char pad) {
		return PAD(str, len, pad, false);
	}

	public static String LPAD(String str, int len, char pad) {
		return PAD(str, len, pad, true);
	}

	public static String PAD(String str, int len, char pad, boolean left) {
		if (str == null)
			str = "";
		if (str.getBytes().length >= len) {
			return new String(str.getBytes(), 0, str.getBytes().length);
		}
		String result = str;
		StringBuffer sb = new StringBuffer();
		int templen = len - result.getBytes().length;

		for (int i = 0; i < templen; i++) {
			sb.append(pad);
		}
		if (left) {
			return sb.toString() + result;
		} else {
			return result + sb.toString();
		}
	}
}
